﻿using System;
using System.Collections.Generic;
using System.Text;
using System.IO;

using Elegant.Ui;

namespace iPdf
{
    /// <summary>
    /// The drop down menu items with click event
    /// </summary>
    [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Naming", "CA1708:IdentifiersShouldDifferByMoreThanCase")]
    class TataDropDownItem
    {
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1051:DoNotDeclareVisibleInstanceFields")]
        public EventHandler Click;
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1051:DoNotDeclareVisibleInstanceFields")]
        protected string text;

        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1051:DoNotDeclareVisibleInstanceFields")]
        protected TataDropDown tataDropDown;

        public virtual bool IsValid
        {
            get
            {
                return !string.IsNullOrEmpty(Text) &&
                            (File.Exists(Text) || Directory.Exists(Text));
            }
        }

        public virtual void ClickThrough(object sender, EventArgs e)
        {
            if (sender is ToggleButton)
            {
                ToggleButton toggleButton = (ToggleButton)sender;
                TataDropDownItem item = (TataDropDownItem) toggleButton.Tag;
                tataDropDown.SelectedItem = item.Text;
            }
        }

        public virtual string Text
        {
            get
            {
                return text;
            }
        }

        public TataDropDownItem(TataDropDown tataDropDown)
        {
            this.tataDropDown = tataDropDown;
            text = string.Empty;
            Click += new EventHandler( ClickThrough );
        }

        public TataDropDownItem(TataDropDown tataDropDown, 
            string text, EventHandler clickHandler)
            : this(tataDropDown)
        {
            this.text = text;
            if (clickHandler != null)
            {
                Click += clickHandler;
            }
        }

        public bool Equals(TataDropDownItem rhs)
        {
            if (rhs == null)
            {
                return false;
            }

            return this.Text == rhs.Text;
        }

        public bool Equals(string str)
        {
            if (!string.IsNullOrEmpty(str))
            {
                return str == this.Text;
            }

            return false;
        }

        public override bool Equals(object obj)
        {
            if (obj == null ||
                !(obj is TataDropDownItem))
            {
                return false;
            }

            return Equals((TataDropDownItem)obj);
        }

        public override int GetHashCode()
        {
            return Text.GetHashCode() | tataDropDown.GetHashCode();
        }
    }
}
